;
(function($) {
	$(document).delegate("input[uppercase]:visible", "keyup", function() {
		var _o = $(this);
		var s = _o.val();
		s = s.toUpperCase();
		_o.val(s);
	});

	$(document).delegate("input[regex]:visible,textarea[regex]:visible", "blur", function() {
		var validate = new Validate(this);
		validate = null;
	});

	/**
	 * 表单整体验证
	 */
	var ValidateForm = function(id) {
		var self = this;
		self._wp = $(id);

		self.flag = true;

		self._input = self._wp.find("input:visible");
		self._textarea = self._wp.find("textarea:visible");

		// 验证input
		self.validateInput();
		// 验证textarea
		self.validateTextarea();
	};

	window["ValidateForm"] = ValidateForm;

	ValidateForm.prototype = {
		// 验证input
		validateInput : function() {
			var self = this;

			self._input.each(function() {
				var validate = new Validate(this);
				// 当验证不通过的时候，才赋值，避免前面验证不通过，后面验证通过了覆盖了前面的标志
				if(!validate.flag){
					self.flag = validate.flag;
				}
				validate = null;

				// 如果false跳出当前循环
				if(!self.flag){
					//return false;
				}
			});
		},

		// 验证textarea
		validateTextarea : function() {
			var self = this;

			self._textarea.each(function() {
				var validate = new Validate(this);
				// 当验证不通过的时候，才赋值，避免前面验证不通过，后面验证通过了覆盖了前面的标志
				if(!validate.flag){
					self.flag = validate.flag;
				}
				validate = null;

				// 如果false跳出当前循环
				if(!self.flag){
					//return false;
				}
			});
		}
	};

	/**
	 * 表单单个验证
	 */
	var Validate = function(obj) {
		var self = this;

		self.flag = true;

		self._o = $(obj);

		var tagName = self._o.get(0).tagName.toLowerCase();
		switch (tagName) {
		case "input": {
			// 验证input
			self.validateInput();
			break;
		}
		case "textarea": {
			// 验证textarea
			self.validateTextarea();
			break;
		}
		}

	};

	window["Validate"] = Validate;

	Validate.prototype = {
		// 验证input
		validateInput : function() {
			var self = this;

			var _o = self._o;

			var val = _o.val();
			var r = _o.attr("regex");
			var tip = _o.attr("tip");
			var required = _o.prop("required");

			var regex = new RegExp(r);
			if (!regex.test(val)) {
				var tipstr = "<span class='form-error-tip'><span class='iconfont icon-cuowu1'></span>  $1</span>";
				tipstr = tipstr.replace(/\$1/g, tip);
				if (_o.parent().find(".form-error-tip").length == 0) {
					_o.parent().append(tipstr);
				}
				self.flag = false; 
			} else {
				if (_o.parent().find(".form-error-tip").length > 0) {
					_o.parent().find(".form-error-tip").remove();
				}
				self.flag = true;
			}
		},

		// 验证input
		validateTextarea : function() {
			var self = this;

			var _o = self._o;

			var val = _o.val();
			var r = _o.attr("regex");
			var tip = _o.attr("tip");
			var required = _o.prop("required");

			// 替换换行符
			val  = val.replace(/\n/g, "");

			var regex = new RegExp(r);
			if (!regex.test(val)) {
				var tipstr = "<span class='form-error-tip' style='display:block; margin-left:230px;'><span class='iconfont icon-cuowu1'></span>  $1</span>";
				tipstr = tipstr.replace(/\$1/g, tip);
				if (_o.parent().find(".form-error-tip").length == 0) {
					_o.parent().append(tipstr);
				}
				self.flag = false; 
			} else {
				if (_o.parent().find(".form-error-tip").length > 0) {
					_o.parent().find(".form-error-tip").remove();
				}
				self.flag = true;
			}
		}
	};
})(jQuery);
